class Ajax {
  constructor(url) {
    var _this = this;
    var request = null;
    if (window.XMLHttpRequest) {
      request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (request != null) {
      this.request = request;
      this.url = url;
      this.request.onload = function () {
        if (_this.request.readyState == 4 && _this.request.status == 200) {
          _this.result = JSON.parse(_this.request.response);
          _this.success(_this.result);
        }
      };

      this.request.onerror = function () {
        _this.result = JSON.parse(_this.request.response);
        _this.error(_this.result);
      };
    } else {
      console.log("您的浏览器不支持 XMLHTTP. 请换个浏览器重试");
    }
  }

  post(options) {
    this.request.open("POST", this.url + options.apiName);
    this.request.send(options.data);
    this.success = options.success;
    this.error = options.error;
  }
}
